package com.andy.blog.dal.service.impl;

import com.andy.blog.common.exception.BlogServiceException;
import com.andy.blog.common.reponse.BlogResultCode;
import com.andy.blog.common.utils.DateUtils;
import com.andy.blog.common.utils.SecurityUtils;
import com.andy.blog.common.utils.TransformUtils;
import com.andy.blog.dal.domain.SysNoticeDO;
import com.andy.blog.dal.mapper.SysNoticeMapper;
import com.andy.blog.dal.service.SysNoticeDalService;
import com.andy.blog.dal.vo.SysNoticeVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by andy on 2023-04-05 11:38
 */
@Service
public class SysNoticeDalServiceImpl implements SysNoticeDalService {

    @Resource
    private SysNoticeMapper noticeMapper;

    /**
     * 构建查询条件
     */
    private LambdaQueryWrapper<SysNoticeDO> buildQueryWrapper(SysNoticeVO sysNoticeVO) {
        LambdaQueryWrapper<SysNoticeDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper
                .eq(StringUtils.isNotBlank(sysNoticeVO.getNoticeType()), SysNoticeDO::getNoticeType, sysNoticeVO.getNoticeType())
                .like(StringUtils.isNotBlank(sysNoticeVO.getNoticeTitle()), SysNoticeDO::getNoticeTitle, sysNoticeVO.getNoticeTitle());

        return lambdaQueryWrapper;
    }

    @Override
    public SysNoticeDO selectById(Long id) {
        return noticeMapper.selectById(id);
    }

    @Override
    public void insert(SysNoticeVO sysNoticeVO) {

        SysNoticeDO sysNoticeDO = TransformUtils.transform(sysNoticeVO, SysNoticeDO.class);

        sysNoticeDO.setCreateBy(SecurityUtils.getUsername());
        sysNoticeDO.setCreateTime(DateUtils.getCurrentDate());

        int row;
        try {
            row = noticeMapper.insert(sysNoticeDO);
        } catch (Exception e) {
            throw new BlogServiceException(BlogResultCode.BLOG_000100, e);
        }

        if (row <= 0) {
            throw new BlogServiceException(BlogResultCode.BLOG_000100);
        }
    }

    @Override
    public void updateById(SysNoticeVO sysNoticeVO) {
        SysNoticeDO sysNoticeDO = TransformUtils.transform(sysNoticeVO, SysNoticeDO.class);

        sysNoticeDO.setUpdateBy(SecurityUtils.getUsername());
        sysNoticeDO.setUpdateTime(DateUtils.getCurrentDate());

        int row;
        try {
            row = noticeMapper.updateById(sysNoticeDO);
        } catch (Exception e) {
            throw new BlogServiceException(BlogResultCode.BLOG_000101, e);
        }

        if (row <= 0) {
            throw new BlogServiceException(BlogResultCode.BLOG_000101);
        }
    }

    @Override
    public int deleteByIds(List<Long> noticeIds) {
        try {
            return noticeMapper.deleteBatchIds(noticeIds);
        } catch (Exception e) {
            throw new BlogServiceException(BlogResultCode.BLOG_000102, e);
        }
    }

    @Override
    public IPage<SysNoticeDO> selectListByPage(SysNoticeVO sysNoticeVO) {
        LambdaQueryWrapper<SysNoticeDO> lambdaQueryWrapper = buildQueryWrapper(sysNoticeVO);

        Page<SysNoticeDO> page = new Page<>(sysNoticeVO.getPageNum(), sysNoticeVO.getPageSize());

        return noticeMapper.selectPage(page, lambdaQueryWrapper);
    }

    @Override
    public List<SysNoticeDO> selectList(SysNoticeVO sysNoticeVO) {
        LambdaQueryWrapper<SysNoticeDO> lambdaQueryWrapper = buildQueryWrapper(sysNoticeVO);

        return noticeMapper.selectList(lambdaQueryWrapper);
    }
}
